<?php
namespace app\zw_admin\controller;
use think\Controller;
use think\Request;
use think\Db;
use think\Session;
class SpecialController extends Controller
{

    public function index()
    {
    	$goods_list = db('activity_goods')
        ->alias('a')
        ->join('drm_selling b','b.selling_id = a.selling_id')
        ->order('off desc')
        ->select();
        $brand_list = db('brand')->where(['brand_id'=>$goods_list[0]['brand_id']])->find();
        $goodsList = [];
        foreach ($goods_list as $key => $value) {
            $value['brand_name'] = $brand_list['brand_name'];
            $goodsList[] = $value;
            // $goodsList[]['brand_name'] = $brand_list['brand_name'];
        }
        // dump($goodsList);
        // exit;
        $this->assign('goods_list',$goodsList);
        return $this->fetch();
    }



    public function add()
    {
        if (Request::instance()->isPost()){
            $goods_list = input('post.');
            // $goods_list['num'] = $goods_list['vertical-spin'];
            // $goods_list['live_unit'] = $goods_list['demo3'];
            // $goods_list['quota'] = $goods_list['demo1'];
            $goods_list['rebate'] = $goods_list['demo3_22'];
            $goods_list['time'] = date('Y-m-d');
            // dump(input('post.'));
            $file = request()->file('image');
            // 移动到框架应用根目录/public/uploads/ 目录下
            // dump($file);
            // exit;
            if($file){
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');

                if($info){
                    //获取图片路径
                    $goods_list['cover_img'] =  'uploads/'.$info->getSaveName();
                }else{
                    // 上传失败获取错误信息
                    echo $file->getError();
                }
            }
            // dump($goods_list);
            // exit;
            
            // unset($goods_list['demo3']);
            unset($goods_list['vertical-spin']);
            unset($goods_list['demo1']);
            unset($goods_list['demo3_22']);
           
            // dump($goods_list);
            // exit;
            // 
            if (db('activity_goods')->insert($goods_list) > 0) {
                return $this->success('添加成功');
            }else{
                return $this->error('添加失败');
            }

            
           
        }else{

            $s_province = Db::name('selling')->distinct(true)->field('s_city')->select();
            

            $brand_list = Db::name('brand')->select();
            $selling_list = Db::name('selling')->select();
            $this->assign('s_province',$s_province);
            $this->assign('brand_list',$brand_list);
            $this->assign('selling_list',$selling_list);
            return $this->fetch();
        }
    }


    /**
     * 多图片处理
     * @return [type] [description]
     */
    public function img()
    {

        // dump(input('post.'));-------------------------------------------
        $file = request()->file('file');

        // dump($file);
        // exit;
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                //获取图片路径
                $list['file'] =  'uploads/'.$info->getSaveName();
            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }

        echo json_encode($list);
        exit;
    }


    /**
     * 三级联动
     */

    public function merchant_ajax()
    {

        $s_province_val = input('post.s_province_val');
        // echo $s_province_val;
        // exit;
        $merchant_list = Db::name('selling')->where(['s_city'=>$s_province_val])->select();
        // dump($merchant_list);
        // exit;
        // $brand_id_list = db('selling_brand')->where(['selling_id'=>$selling_id])->select();
        // // dump()
        // $brand_id = '';
        // foreach ($brand_id_list as $key => $value) {
        //   $brand_id .= "`brand_id`="."'".$value['brand_id']."'"." or ";

        // }
        // $brand_id = rtrim($brand_id,'or ');
        // // echo $brand_id;
        // // exit;
        // $brand_list = db('brand')->where($brand_id)->select();
        // // dump($selling_list);
        return $merchant_list;
    }



    /**
     * 三级联动
     */

    public function merchant_ajax_to()
    {

        $selling_id = input('post.selling_id');
        // echo $selling_id;
        // exit;
        // $merchant_list = Db::name('selling')->where(['s_city'=>$s_province_val])->select();
        // dump($merchant_list);
        // exit;
        $brand_id_list = db('selling_brand')->where(['selling_id'=>$selling_id])->select();
        // // dump()
        $brand_id = '';
        foreach ($brand_id_list as $key => $value) {
          $brand_id .= "`brand_id`="."'".$value['brand_id']."'"." or ";

        }
        $brand_id = rtrim($brand_id,'or ');
        // echo $brand_id;
        // exit;
        $brand_list = db('brand')->where($brand_id)->select();
        // dump($selling_list);
        return $brand_list;
    }




    /**
     * 删除特价
     * @return [type] [description]
     */
    public function del()
    {   
        $id = input('post.gid');
        $del_count = Db::name('activity_goods')->where('g_id',$id)->delete();
        if ($del_count > 0) {
            return '111';
        }else{
            return '222';
        }
        // dump(input('post.id'));
        // exit;
    }
}